package middleware

import (
	"github.com/gin-gonic/gin"
	"inspector/internal/config"
	"inspector/pkg/code"
	"inspector/pkg/errors"
)

var expectedKey = config.Get().GetAtomConfig().ApiKey

func ValidateAtomGitAPIKey() gin.HandlerFunc {
	return func(c *gin.Context) {
		apiKey := c.GetHeader("X-ATOMGIT-API")

		if apiKey != expectedKey {
			c.Error(errors.New(code.NewErrAuthFailed(), "Authentication failed"))
			c.Abort()
			return
		}

		c.Next()
	}
}
